package com.myrobot.model;

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;

public class PointModel {
	private Map<String, Point> mPoints = null;

	public PointModel(Map<String, Point> points) {
		mPoints = points;
	}

	public PointModel(String data) {
		mPoints = new HashMap<String, Point>();
		try {
			JSONObject root = new JSONObject(data);
			for (String key : JSONObject.getNames(root)) {
				JSONObject item = root.getJSONObject(key);
				mPoints.put(key, new Point(item.getInt("x"), item.getInt("y")));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public String toString() {
		String str = "";
		if (mPoints != null) {
			JsonObject root = new JsonObject();
			for (String key : mPoints.keySet()) {
				JsonObject item = new JsonObject();
				item.addProperty("x", mPoints.get(key).x);
				item.addProperty("y", mPoints.get(key).y);
				root.add(key, item);
			}
			str = root.toString();
		}
		return str;
	}

	public Map<String, Point> getPoints() {
		return mPoints;
	}
}
